#include <stdio.h>

static __inline int read_ebp()
{
  int ebp;

  __asm__ __volatile("movl %%ebp, %0" : "=r" (ebp));

  return ebp;
}

#define my_backtrace()          \
    do {                        \
        int bp, ip;                                         \
        __asm__ __volatile("movl %%ebp, %0" : "=r" (bp));   \
        ip = *((int*)bp+1);                                 \
        do {                                                \
            printf("0x%08x\n", ip);                         \
            bp = *((int*)bp);                               \
            if (bp != 0) {                                  \
                ip = *((int*)bp+1);                         \
            }                                               \
        } while (bp != 0);                                  \
    } while (0);


static __inline void backtrace()
{
    int bp, ip;

    bp = read_ebp();
    ip = *((int*)bp+1);
    do {
        printf("0x%08x 0x%08x\n", bp, ip);
        bp = *((int*)bp);
        if (bp != 0) {
            ip = *((int*)bp+1);
        }
    } while (bp != 0);
}

void bar()
{
    my_backtrace();
}

void foo()
{
  bar();
}

int main()
{
   foo(); 
}

